[][src]Crate bech32

Encoding and decoding Bech32 format

Bech32 is a 5-bit (base-32) encoding scheme that produces strings that comprise a human-readable part, a separator, a data part, and a checksum. The encoding implements a BCH code that guarantees error detection of up to four characters with less than 1 in 1 billion chance of failing to detect more errors.

The Bech32 encoding was originally formulated in BIP-0173

Examples

use bech32::Bech32;

let b = Bech32::new_check_data("bech32".into(), vec![0x00, 0x01, 0x02]).unwrap();
let encoded = b.to_string();
assert_eq!(encoded, "bech321qpz4nc4pe".to_string());

let c = encoded.parse::<Bech32>();
assert_eq!(b, c.unwrap());

If the data is already range-checked the Bech32::new function can be used which will never return Err(Error::InvalidData).

use bech32::{Bech32, u5, ToBase32};

// converts base256 data to base32 and adds padding if needed
let checked_data: Vec<u5> = [0xb4, 0xff, 0xa5].to_base32();

let b = Bech32::new("bech32".into(), checked_data).expect("hrp is not empty");
let encoded = b.to_string();

assert_eq!(encoded, "bech321knl623tk6v7".to_string());

Structs

Bech32

Grouping structure for the human-readable part and the data part of decoded Bech32 string.

u5

Integer in the range 0..32

Enums

Error

Error types for Bech32 encoding / decoding

Traits

CheckBase32

A trait to convert between u8 arrays and u5 arrays without changing the content of the elements, but checking that they are in range.

FromBase32

Parse/convert base32 slice to Self. It is the reciprocal of ToBase32.

ToBase32

A trait for converting a value to a type T that represents a u5 slice.

Functions

convert_bits

Convert between bit sizes